

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Han Qiu">
  <meta name="keywords" content="">
  
    <meta name="description" content="一、容器属性1. display：是否采用flex布局 flex：指定容器采用flex布局，容器元素默认为块级元素 inline-flex：指定容器采用flex布局，容器元素为行内元素  2. flex-direction：主轴的方向，及项目的排列方向 row（默认值）：主轴为水平方向，起点在左端 row-reverse：主轴为水平方向，起点在右端 column：主轴为垂直方向，起点在上沿 col">
<meta property="og:type" content="article">
<meta property="og:title" content="flex弹性布局">
<meta property="og:url" content="http://example.com/2022/02/14/flex%E5%BC%B9%E6%80%A7%E5%B8%83%E5%B1%80/index.html">
<meta property="og:site_name" content="寒秋の博客">
<meta property="og:description" content="一、容器属性1. display：是否采用flex布局 flex：指定容器采用flex布局，容器元素默认为块级元素 inline-flex：指定容器采用flex布局，容器元素为行内元素  2. flex-direction：主轴的方向，及项目的排列方向 row（默认值）：主轴为水平方向，起点在左端 row-reverse：主轴为水平方向，起点在右端 column：主轴为垂直方向，起点在上沿 col">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-02-14T03:25:01.000Z">
<meta property="article:modified_time" content="2022-02-14T03:27:40.958Z">
<meta property="article:author" content="Han Qiu">
<meta property="article:tag" content="布局">
<meta name="twitter:card" content="summary_large_image">
  
  
  <title>flex弹性布局 - 寒秋の博客</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 6.0.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>blogs</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="flex弹性布局">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2022-02-14 11:25" pubdate>
        2022年2月14日 上午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      2.4k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      20 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">flex弹性布局</h1>
            
            <div class="markdown-body">
              <h4 id="一、容器属性"><a href="#一、容器属性" class="headerlink" title="一、容器属性"></a>一、容器属性</h4><h5 id="1-display：是否采用flex布局"><a href="#1-display：是否采用flex布局" class="headerlink" title="1. display：是否采用flex布局"></a>1. display：是否采用flex布局</h5><ul>
<li>flex：指定容器采用flex布局，容器元素默认为块级元素</li>
<li>inline-flex：指定容器采用flex布局，容器元素为行内元素</li>
</ul>
<h5 id="2-flex-direction：主轴的方向，及项目的排列方向"><a href="#2-flex-direction：主轴的方向，及项目的排列方向" class="headerlink" title="2. flex-direction：主轴的方向，及项目的排列方向"></a>2. flex-direction：主轴的方向，及项目的排列方向</h5><ul>
<li>row（默认值）：主轴为水平方向，起点在左端</li>
<li>row-reverse：主轴为水平方向，起点在右端</li>
<li>column：主轴为垂直方向，起点在上沿</li>
<li>column-reverse：主轴为垂直方向，起点在下沿</li>
</ul>
<h5 id="3-flex-wrap：项目如何换行"><a href="#3-flex-wrap：项目如何换行" class="headerlink" title="3. flex-wrap：项目如何换行"></a>3. flex-wrap：项目如何换行</h5><ul>
<li>nowrap（默认）：不换行</li>
<li>wrap：换行，第一行在上方</li>
<li>wrap-reverse：换行，第一行在下方</li>
</ul>
<h5 id="4-flex-flow：flex-direction，flex-wrap属性的简写"><a href="#4-flex-flow：flex-direction，flex-wrap属性的简写" class="headerlink" title="4. flex-flow：flex-direction，flex-wrap属性的简写"></a>4. flex-flow：flex-direction，flex-wrap属性的简写</h5><ul>
<li>例如：flex-flow: row wrap;</li>
</ul>
<h5 id="5-justify-content：主轴上的对齐方式"><a href="#5-justify-content：主轴上的对齐方式" class="headerlink" title="5. justify-content：主轴上的对齐方式"></a>5. justify-content：主轴上的对齐方式</h5><ul>
<li>flex-start（默认值）：左对齐</li>
<li>flex-end：右对齐</li>
<li>center： 居中</li>
<li>space-between：两端对齐，项目之间的间隔都相等</li>
<li>space-around：每个项目两侧的间隔相等。所以，项目之间的间隔比项目与边框的间隔大一倍</li>
<li>space-evenly：主轴所有间隔相等。项目之间、项目与边框之间的间隔都相等</li>
</ul>
<h5 id="6-align-items：项目在交叉轴上的对齐方式"><a href="#6-align-items：项目在交叉轴上的对齐方式" class="headerlink" title="6. align-items：项目在交叉轴上的对齐方式"></a>6. align-items：项目在交叉轴上的对齐方式</h5><ul>
<li>flex-start：交叉轴的起点对齐</li>
<li>flex-end：交叉轴的终点对齐</li>
<li>center：交叉轴的中点对齐</li>
<li>baseline: 项目的第一行文字的基线对齐</li>
<li>stretch（默认值）：如果项目未设置高度或设为auto，将占满整个容器的高度</li>
<li>normal：在flex布局中，效果和stretch一样</li>
</ul>
<h5 id="7-align-content：决定多行的flex-items在交叉轴上的对齐方式，即多根轴线的对齐方式（如果项目只有一根轴线，该属性不起作用）"><a href="#7-align-content：决定多行的flex-items在交叉轴上的对齐方式，即多根轴线的对齐方式（如果项目只有一根轴线，该属性不起作用）" class="headerlink" title="7. align-content：决定多行的flex-items在交叉轴上的对齐方式，即多根轴线的对齐方式（如果项目只有一根轴线，该属性不起作用）"></a>7. align-content：决定多行的flex-items在交叉轴上的对齐方式，即多根轴线的对齐方式（如果项目只有一根轴线，该属性不起作用）</h5><ul>
<li>flex-start：与交叉轴的起点对齐</li>
<li>flex-end：与交叉轴的终点对齐</li>
<li>center：与交叉轴的中点对齐</li>
<li>space-between：与交叉轴两端对齐，轴线之间的间隔平均分布</li>
<li>space-around：每根轴线两侧的间隔都相等。所以，轴线之间的间隔比轴线与边框的间隔大一倍</li>
<li>space-evenly：主轴所有间隔相等。项目之间、项目与边框之间的间隔都相等</li>
<li>stretch（默认值）：轴线占满整个交叉轴</li>
</ul>
<h5 id="8-align-items-和-align-content-区别"><a href="#8-align-items-和-align-content-区别" class="headerlink" title="8. align-items 和 align-content 区别"></a>8. align-items 和 align-content 区别</h5><ul>
<li>都是设置侧轴（交叉轴，非主轴）上的对齐方式</li>
<li>align-items适用于单行情况下，只有上对齐、下对齐、居中和拉伸</li>
<li>align-content适用于换行（多行）的情况下（单行情况无效），可以设置上对齐、下对齐、居中、拉伸以及平均分配剩余空间等属性</li>
<li>总结就是单行找align-items，多行找align-content</li>
</ul>
<h4 id="二、项目属性"><a href="#二、项目属性" class="headerlink" title="二、项目属性"></a>二、项目属性</h4><h5 id="1-order：项目的排列顺序"><a href="#1-order：项目的排列顺序" class="headerlink" title="1. order：项目的排列顺序"></a>1. order：项目的排列顺序</h5><ul>
<li>order数值越小，排列越靠前，默认为0，-1比0小</li>
</ul>
<h5 id="2-flex-grow：项目的放大比例"><a href="#2-flex-grow：项目的放大比例" class="headerlink" title="2. flex-grow：项目的放大比例"></a>2. flex-grow：项目的放大比例</h5><ul>
<li>默认为0，即如果存在剩余空间也不放大，可以设置任意非负数字（正小数、正整数、0）。当flex-container在max axis方向上有剩余size时，flex-grow属性才会生效。flex-items扩展后的最终size不能超过max-width/max-height</li>
<li>如果所有flex-items的flex-grow总和sum超过1，每个flex-items扩展的size为：flex-container的剩余size * flex-grow / sum</li>
<li>如果所有flex-items的flex-grow总和sum不超过1，每个flex-items扩展的size为：flex-container的剩余size * flex-grow</li>
</ul>
<h5 id="3-flex-shrink：项目的缩小比例"><a href="#3-flex-shrink：项目的缩小比例" class="headerlink" title="3. flex-shrink：项目的缩小比例"></a>3. flex-shrink：项目的缩小比例</h5><ul>
<li>默认为1，即如果空间不足，将该项目缩小，可以设置任意非负数字（正小数、正整数、0）。当flex-items在max axis方向上超过了flex-container的size，flex-shrink属性才会生效。flex-items收缩后的最终size不能小于min-width/min-height</li>
<li>如果所有flex-items的flex-shrink总和sum超过1，每个flex-items收缩的size为：flex-items超出flex-container的size * flex-shrink / sum</li>
<li>如果所有flex-items的flex-shrink总和sum不超过1，每个flex-items收缩的size为：flex-items超出flex-container的size * flex-shrink</li>
</ul>
<h5 id="4-flex-basis：在分配多余空间之前，项目占据的主轴空间"><a href="#4-flex-basis：在分配多余空间之前，项目占据的主轴空间" class="headerlink" title="4. flex-basis：在分配多余空间之前，项目占据的主轴空间"></a>4. flex-basis：在分配多余空间之前，项目占据的主轴空间</h5><ul>
<li>浏览器根据这个属性，计算主轴是否有多余空间，默认值为auto，即项目本来的大小</li>
<li>决定flex-items最终base size的因素，从优先级高到低：max-width/max-height/min-width/min-height -&gt; flex-basis -&gt; width/height -&gt; 内容本身的size</li>
</ul>
<h5 id="5-flex：flex-grow，flex-shrink，flex-basis属性的简写"><a href="#5-flex：flex-grow，flex-shrink，flex-basis属性的简写" class="headerlink" title="5. flex：flex-grow，flex-shrink，flex-basis属性的简写"></a>5. flex：flex-grow，flex-shrink，flex-basis属性的简写</h5><ul>
<li>默认值为0 1 auto，后两个属性可选</li>
<li>该属性有两个快捷值：auto -&gt; 1 1 auto，none -&gt; 0 0 auto</li>
<li>优先使用该属性，而不是单独写三个分离的属性，因为浏览器会推算相关值</li>
</ul>
<h5 id="6-align-self：单个项目与其他项目不一样的对齐方式，可覆盖align-items属性"><a href="#6-align-self：单个项目与其他项目不一样的对齐方式，可覆盖align-items属性" class="headerlink" title="6. align-self：单个项目与其他项目不一样的对齐方式，可覆盖align-items属性"></a>6. align-self：单个项目与其他项目不一样的对齐方式，可覆盖align-items属性</h5><ul>
<li>默认值为auto，表示继承父元素的align-items属性，如果没有父元素，则等同于stretch</li>
<li>该属性可能取6个值，除了auto，其他都与align-items属性完全一致</li>
</ul>
<blockquote>
<p>参考文档：<a target="_blank" rel="noopener" href="https://www.ruanyifeng.com/blog/2015/07/flex-grammar.html">阮一峰老师的flex布局教程</a></p>
</blockquote>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/css/">css</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/%E5%B8%83%E5%B1%80/">布局</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/02/14/grid%E7%BD%91%E6%A0%BC%E5%B8%83%E5%B1%80/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">grid网格布局</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/01/26/%E4%BD%BF%E7%94%A8vscode%E6%9E%84%E5%BB%BAuniapp%E9%A1%B9%E7%9B%AE/">
                        <span class="hidden-mobile">使用vscode构建uniapp项目</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
